<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="520px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="姓名" prop="personName">
        <el-input v-model="form.personName" style="width: 370px;"/>
      </el-form-item>
       <el-form-item label="账号" prop="account">
        <el-input v-model="form.account" style="width: 370px;" />
      </el-form-item>

       <el-form-item label="手机号"  prop="phone">
        <el-input v-model="form.phone" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="密码" prop="personPassword">
        <el-input 
            type="password"
            placeholder="请输入密码" 
            maxlength="18"  
　　        :show-password="true" 
        v-model="form.personPassword" style="width: 370px;"/>
      </el-form-item>
      
      <el-form-item label="所属服务商" prop="serviceproviderId">
       <el-select v-model="form.serviceproviderId" placeholder="服务商" v-on:change="indexSelect($event)">
           <el-option v-for="(item,index) in serviceproItem" :key="index" :label="item.seproName" :value="item.id">
           </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="是否管理员"  prop="state">
        <el-radio v-for="item in admins" :key="item.id" v-model="form.state" :label="item.id">{{ item.name }}</el-radio>
      </el-form-item>
      <!-- <el-form-item label="服务商*id" >
        <el-input v-model="form.serviceproviderId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="服务商名称*名称" >
        <el-input v-model="form.serviceproviderName" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" >
        <el-input v-model="form.creatTime" style="width: 370px;"/>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" v-show="isView===true" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { add, edit } from '@/api/tServiceproviderPerson'
import { debuglog } from 'util';
import md5 from 'md5';
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    isView: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        personName: '',
        personPassword: '',
        account: '',
        phone: '',
        serviceproviderId: '',
        serviceproviderName: '',
        creatTime: '',
        state: 1
      },
       admins: [
        {
          id: 1,
          name: "是"
        },
        {
          id: 0,
          name: "否"
        }
      ],
      serviceproItem:[],
      rules: {
        personName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        account: [
          { required: true, message: "请输入账号", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入电话号码", trigger: "blur" }
        ],
        personPassword: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ],
        serviceproviderId: [
          { required: true, message: "请选择服务商", trigger: "blur" }
        ]
      }
    }
  },
  mounted:function(){
     this.tServiceproviderInfoAll();
  },
  methods: {
    tServiceproviderInfoAll(){
        this.getAllService().then(res => {
          this.serviceproItem=res;
        //根据id再进行全部查询
      })
    },
    getAllService() {
      return request({
        url: 'api/tServiceproviderInfoAll',
        method: 'get'
      })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        } else {
          return false;
        }
      });
      this.loading = true
      // debugger;
      
      this.form.personPassword=md5(this.ruleForm.personPassword)
      console.log(this.form);
      if (this.isAdd) {
         this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        personName: '',
        personPassword: '',
        account: '',
        phone: '',
        serviceproviderId: '',
        serviceproviderName: '',
        creatTime: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
